package com.linxz.wandnroid.appui;

/*
  @author Linxz
 * 创建日期：2020年03月01日 16:10
 * version：v4.5.4
 * 描述：
 */

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.linxz.evens.ARouterURLConfig;
import com.linxz.framework.base.BaseMVPActivity;
import com.linxz.framework.utils.ShareSDKManager;
import com.linxz.ui.GuiHelper;
import com.linxz.ui.dialogs.DialogHelper;
import com.linxz.ui.listener.WebSaveListener;
import com.linxz.ui.widgets.LinxzActionBar;
import com.linxz.ui.widgets.LinxzRefreshLayout;
import com.linxz.ui.widgets.SmartLayout;
import com.linxz.wandnroid.R;
import com.linxz.wandnroid.adapter.ArticleAdapter;
import com.linxz.wandnroid.appui.view.CategoryArticlesView;
import com.linxz.wandnroid.pojo.WanAndroidArticleBean;
import com.linxz.wandnroid.presenter.CategoryArticlesPresenter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

@Route(path = ARouterURLConfig.WAN_ANDROID_CATEGORY_ARTICLE)
public class CategoryArticlesActivity extends BaseMVPActivity<CategoryArticlesPresenter> implements CategoryArticlesView {

    private LinxzActionBar actionBar;
    private SmartLayout layoutSmart;
    private LinxzRefreshLayout layoutRefresh;
    private ArticleAdapter articleAdapter;
    @Autowired(name = "name")
    String categoryName;
    @Autowired(name = "cid")
    int cid;

    @Override
    protected CategoryArticlesPresenter createPresenter() {
        return new CategoryArticlesPresenter(this,lifecycleSubject);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wanadroid_act_category_article);
        initUI();
        setListener();
        initData();
    }

    @Override
    protected void initUI() {
        actionBar=findViewById(R.id.actionBar);
        layoutSmart=findViewById(R.id.layoutSmart);
        layoutRefresh=findViewById(R.id.layoutRefresh);
        RecyclerView rvArticle = findViewById(R.id.rvArticle);
        articleAdapter=new ArticleAdapter(mvpPresenter.getArticleBeans());
        GuiHelper.initRecycleView(activity, rvArticle,articleAdapter,1);
        articleAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        actionBar.setTittle(categoryName);
        layoutSmart.showLoadingView();
    }

    @Override
    protected void initData() {
        mvpPresenter.refreshData(cid);
    }

    @Override
    protected void setListener() {
        super.setListener();
        layoutRefresh.setOnRefreshListener(new LinxzRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mvpPresenter.refreshData(cid);
            }
        });
        layoutRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mvpPresenter.loadMoreData();
            }
        });
        articleAdapter.setOnItemClickListener(new ArticleAdapter.OnItemClickListener() {
            @Override
            public void onClick(WanAndroidArticleBean item) {
                WanAndroidWebViewActivity.launch(activity,item.getLink());
            }

            @Override
            public void onLongClick(final WanAndroidArticleBean item) {
                DialogHelper
                        .builder()
                        .build()
                        .webViewShare(activity, new WebSaveListener() {
                            @Override
                            public void onWxFriends() {
                                ShareSDKManager.wxFriendsShare(item.getLink(),item.getTitle(),"我正在使用\"虾玩APP\"给你分享一篇有趣的文章");
                            }

                            @Override
                            public void onWxCircle() {
                                ShareSDKManager.wxZoneShare(item.getLink(),item.getTitle(),"我正在使用\"虾玩APP\"给你分享一篇有趣的文章");
                            }

                            @Override
                            public void onCollect() {
                                 showToast("该功能还未开发");
                            }

                            @Override
                            public void onCopy() {
                                 GuiHelper.copy(activity,item.getLink());
                            }
                        });
            }
        });
    }

    @Override
    public void onLoadSuccess() {
        layoutRefresh.finishRefresh();
        layoutRefresh.finishLoadMore();
        layoutSmart.showNormal();
        articleAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadError(String message, boolean showErrorLayout) {
        layoutRefresh.finishRefresh();
        layoutRefresh.finishLoadMore();
        if (showErrorLayout){
            layoutSmart.showErrorView();
        }else{
            showToast(message);
        }
    }

    @Override
    public void onDataEmpty(boolean showEmptyLayout) {
        layoutRefresh.finishRefresh();
        layoutRefresh.finishLoadMore();
        if (showEmptyLayout){
            layoutSmart.showEmptyView();
        }else{
            showToast("已没有更多数据");
            layoutRefresh.setNoMoreData(true);
        }

    }
}
